// pages/my/chongzhi/dochongzhi.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        price:0,
        // yue:0,
        yajin:0
    },
    onLoad:function(e){
        var that = this;
        this.setData({
            yajin:getApp().globalData.userInfo.yajin
        })
    },
    bindinput:function(e){
        this.setData({
            price:e.detail.value
        })
    },
    zhifu:function(e){
        var that = this;
        if(that.data.price<=0){
            wx.showToast({
                title:'请填入充值金额',
                icon: 'none',
                duration:3000
            })
            return ;
        }
        // console.log(getApp().globalData.userInfo.openid)
        wx.request({
            url:app.data.server + '/index.php/Tongcheng/Book/chongzhiyajin',
            data:{
                price:this.data.price,
                openid:getApp().globalData.userInfo.openid,
                uid:getApp().globalData.userInfo.uid
            },
            success:function(d){

                console.log(d.data);

                wx.requestPayment(
                    {
                        'timeStamp': d.data.timeStamp,
                        'nonceStr': d.data.nonceStr,
                        'package': d.data.package,
                        'signType': 'MD5',
                        'paySign': d.data.paySign,
                        'success':function(res){
                            console.log(res)
                            wx.showToast({
                                title:'购买成功',
                                icon: 'success',
                                duration:3000
                            })
                        },
                        'fail':function(res){
                            console.log(res)
                            if('requestPayment:fail cancel'==res.errMsg){
                                wx.showToast({
                                    title:'用户取消支付',
                                    icon: 'none',
                                    duration:3000
                                })
                            }else{
                                wx.showToast({
                                    title:'支付失败',
                                    icon: 'none',
                                    duration:3000
                                })
                            }
                        },
                        'complete':function(res){
                            // wx.showToast({
                            //     title:'购买成功',
                            //     icon: 'success',
                            //     duration:3000,
                            //     success:function(e){
                            //         that.showJieSuan();
                            //     }
                            // })
                        }
                    })

            }
        })
    }
})